﻿CREATE PROCEDURE [dbo].[proc_Finance_Split_Create]
	(
		@Id int,
		@txt decimal(18,2),
		@Description nvarchar(50)
	)
AS

BEGIN TRANSACTION--开始事务
DECLARE @errorSun INT --定义错误计数器
SET @errorSun=0 --没错为0
                --
--DECLARE @temp NVARCHAR(300),@i INT,@temp_len INT 
--SET @i=0
--SET @temp_len=0
--WHILE (@i<LEN(@Description))
--	BEGIN
--		--@Description内容格式为100.00^收入拆分~200.00^收入拆分200~
--		--@temp为以每个~作为分隔的数据，拆分出的每一项（100.00^收入拆分~）
--		set @temp=SUBSTRING(@Description, @temp_len+1,CHARINDEX('~',@Description,@temp_len+1)-@temp_len)
--		--@i为循环条件，每次加上@temp的长度，当长度大于@Description的长度时跳出循环
--		SET @i=@i+LEN(@temp)
--		--@temp_len为下次循环时，整个字符串@Description中第几个位置开始找“~”
--		SET @temp_len=CHARINDEX('~',@Description,@temp_len+1)
		
--		-----插入表
--		--
		
--		Insert Into Finance_RunningAccount
--	(
--		CreateDate,
--		TradeDate,
--		Summary,
--		AccountsTypeId,
--		AccountsTypeName,
--		ParentId,
--		ParentName,
--		SubjectId,
--		SubjectName,
--		PaymentTypeId,
--		PaymentTypeName,
--		BankId,
--		BankName,
--		Income,
--		Outlay,
--		Agent,
--		AgentName,
--		UserName,
--		EName,
--		CompanyId,
--		Description,
--		BillNumber,
--		Renling
--	)
--	SELECT  
	
--		CreateDate,
--		TradeDate,
--		Summary,
--		AccountsTypeId,
--		AccountsTypeName,
--		ParentId,
--		ParentName,
--		SubjectId,
--		SubjectName,
--		PaymentTypeId,
--		PaymentTypeName,
--		BankId,
--		BankName,
--		CONVERT(decimal(18,2), SUBSTRING(@temp ,0,CHARINDEX('^',@temp,0))),
--		Outlay,
--		Agent,
--		AgentName,
--		UserName,
--		EName,
--		CompanyId,
--		SUBSTRING(@temp ,CHARINDEX('^',@temp,0)+1,len(@temp)-CHARINDEX('^',@temp,0)-1),
--		BillNumber,
--		Renling
--	from Finance_RunningAccount	WHERE id = (SELECT RId FROM Finance_RunningAccountCopy WHERE id=@id)

--if @@ROWCOUNT<1
--		begin
--			set @errorSun=@errorSun+1
--			goto e;
--		END
		
--	Declare @RId int
--	Select @RId=@@IDENTITY

--	Insert Into Finance_RunningAccountCopy
--	(
--		RId
--		,CreateDate
--		,TradeDate
--		,Summary
--		,AccountsTypeId
--		,AccountsTypeName
--		,PaymentTypeId
--		,PaymentTypeName
--		,BankId
--		,BankName
--		,Income
--		,Outlay
--		,UserName
--		,EName
--		,CompanyId
--		,Description
--		,BillNumber
--	)
--	Select
--		@RId
--		,GetDate()
--		,TradeDate
--		,Summary
--		,AccountsTypeId
--		,AccountsTypeName
--		,PaymentTypeId
--		,PaymentTypeName
--		,BankId
--		,BankName
--		,CONVERT(decimal(18,2),SUBSTRING(@temp ,0,CHARINDEX('^',@temp,0)))
--		,Outlay
--		,UserName
--		,EName
--		,CompanyId
--		,SUBSTRING(@temp ,CHARINDEX('^',@temp,0)+1,len(@temp)-CHARINDEX('^',@temp,0)-1)
--		,BillNumber
--	From Finance_RunningAccountCopy Where Id=@Id
--	if @@ROWCOUNT<1
--		begin
--			set @errorSun=@errorSun+1		
--			goto e;
--		END	
		
--		--插入表END	
		
--	END

                
      
Insert Into Finance_RunningAccount
	(
		CreateDate,
		TradeDate,
		Summary,
		AccountsTypeId,
		AccountsTypeName,
		ParentId,
		ParentName,
		SubjectId,
		SubjectName,
		PaymentTypeId,
		PaymentTypeName,
		BankId,
		BankName,
		Income,
		Outlay,
		Agent,
		AgentName,
		UserName,
		EName,
		CompanyId,
		Description,
		BillNumber,
		Renling
	)
	SELECT  
	
		CreateDate,
		TradeDate,
		Summary,
		AccountsTypeId,
		AccountsTypeName,
		ParentId,
		ParentName,
		SubjectId,
		SubjectName,
		PaymentTypeId,
		PaymentTypeName,
		BankId,
		BankName,
		@txt,
		Outlay,
		Agent,
		AgentName,
		UserName,
		EName,
		CompanyId,
		@Description,
		BillNumber,
		Renling
	from Finance_RunningAccount	WHERE id = (SELECT RId FROM Finance_RunningAccountCopy WHERE id=@id)

if @@ROWCOUNT<1
		begin
			set @errorSun=@errorSun+1
			goto e;
		END
		
	Declare @RId int
	Select @RId=@@IDENTITY

	Insert Into Finance_RunningAccountCopy
	(
		RId
		,CreateDate
		,TradeDate
		,Summary
		,AccountsTypeId
		,AccountsTypeName
		,PaymentTypeId
		,PaymentTypeName
		,BankId
		,BankName
		,Income
		,Outlay
		,UserName
		,EName
		,CompanyId
		,Description
		,BillNumber
	)
	Select
		@RId
		,GetDate()
		,TradeDate
		,Summary
		,AccountsTypeId
		,AccountsTypeName
		,PaymentTypeId
		,PaymentTypeName
		,BankId
		,BankName
		,@txt
		,Outlay
		,UserName
		,EName
		,CompanyId
		,@Description
		,BillNumber
	From Finance_RunningAccountCopy Where Id=@Id
	if @@ROWCOUNT<1
		begin
			set @errorSun=@errorSun+1		
			goto e;
		END


--DELETE FROM Finance_RunningAccount WHERE Id IN (SELECT RId FROM Finance_RunningAccountCopy WHERE id=@id)
--		if @@ROWCOUNT<1
--		begin
--			set @errorSun=@errorSun+1		
--			goto e;
--		END
--Delete From Finance_RunningAccountCopy Where Id=@Id
	--if @@ROWCOUNT<1
	--	begin
	--		set @errorSun=@errorSun+1		
	--		goto e;
	--	END



		
 e:
    PRINT @errorSun
	if @errorSun>0
	begin
		rollback TRANSACTION
	end
	else
	begin
		commit TRANSACTION
	END	
		
		
	RETURN
